package biz.bbtec.sbm.issuetracking.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.ejb.Remove;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import biz.bbtec.sbm.issuetracking.bean.AttachmentBean;
import biz.bbtec.sbm.issuetracking.bean.NoteBean;
import biz.bbtec.sbm.issuetracking.bean.WorkBean;
import biz.bbtec.sbm.issuetracking.factory.AttachmentFactory;
import biz.bbtec.sbm.issuetracking.service.AttachmentService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AttachmentAction extends ActionSupport implements SessionAware {

	private AttachmentBean attachment = new AttachmentBean();
	private List<AttachmentBean> attachmentlist = new ArrayList<AttachmentBean>();
	private NoteBean note;
	private WorkBean work = new WorkBean();

	private Map session;

	private File image;
	private String imageFileName;
	private String imageFileContent;

	private InputStream inputStream;
	private String fileName;

	public WorkBean getWork() {
		return work;
	}

	public void setWork(WorkBean work) {
		this.work = work;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileContent() {
		return imageFileContent;
	}

	public void setImageFileContent(String imageFileContent) {
		this.imageFileContent = imageFileContent;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public NoteBean getNote() {
		return note;
	}

	public void setNote(NoteBean note) {
		this.note = note;
	}

	public List<AttachmentBean> getAttachmentlist() {
		return attachmentlist;
	}

	public void setAttachmentlist(List<AttachmentBean> attachmentlist) {
		this.attachmentlist = attachmentlist;
	}

	public AttachmentBean getAttachment() {
		return attachment;
	}

	public void setAttachment(AttachmentBean attachment) {
		this.attachment = attachment;
	}

	@SuppressWarnings("unchecked")
	public String showallAttachment() throws Exception {
		if (attachment.getNoteid() != 0) {
			note = new NoteBean();
			note.setNoteid(attachment.getNoteid());
		}
		AttachmentService attachmentservice = AttachmentFactory.getInstance()
				.creatAttachmentService();
		attachmentlist = attachmentservice.ShowAllAttachemnt(note);
		if (attachmentlist.size() == 0) {
			this.addActionError("没有附件！");
			this.session.put("attnote", note.getNoteid());
			return INPUT;
		} else {
			int attnoteid = attachmentlist.get(0).getNoteid();
			this.session.put("attnote", attnoteid);
			return SUCCESS;
		}
	}

	public String showallAtt() throws Exception {
		Map session = ActionContext.getContext().getSession();
		if (work.getWpid() == 0) {
			work.setWpid((Integer) (session.get("attwpid")));
		}
		AttachmentService attachmentservice = AttachmentFactory.getInstance()
				.creatAttachmentService();
		attachmentlist = attachmentservice.ShowAllAtt(work);
		if (attachmentlist.size() == 0) {
			this.addActionError("没有附件，请添加！");
			this.session.put("attwpid", work.getWpid());
			return INPUT;
		} else {
			int attwpid = attachmentlist.get(0).getWpid();
			this.session.put("attwpid", attwpid);
			return SUCCESS;
		}
	}

	public String addAttachment() throws Exception {

		Random rnd = new Random();
		int p = rnd.nextInt(20);
		String num = Integer.toString(p);

		String path = ServletActionContext.getServletContext().getRealPath(
				"/images");

		imageFileName = num + "." + imageFileName;

		File file = new File(path + "//" + imageFileName);
		FileUtils.copyFile(image, file);

		AttachmentService attachmentservice = AttachmentFactory.getInstance()
				.creatAttachmentService();
		if (!attachmentservice.AddAttachment(imageFileName, attachment)) {
			this.addActionError("添加失败！");
			return INPUT;
		}

		return SUCCESS;

	}

	public String download() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath(
				"images");
		setFileName(attachment.getTitle());
		File file = new File(path + "\\" + fileName);
		attachment.getTitle();
		inputStream = new FileInputStream(file);
		return SUCCESS;

	}

	public String deleteAttachment() throws Exception {
		AttachmentService attachmentservice = AttachmentFactory.getInstance()
				.creatAttachmentService();

		String title = attachmentservice.ExitAttachment(attachment);
		if (title.equalsIgnoreCase("")) {
			this.addActionError("不存在该文件！");
			return INPUT;
		} else {
			if (!attachmentservice.DeleteAttachment(attachment)) {
				this.addActionError("出错啦！");
				return INPUT;
			} else {
				String path = ServletActionContext.getServletContext()
						.getRealPath("images");
				setFileName(attachment.getTitle());
				File f = new File(path + "\\" + title);
				if (f.exists()) {
					f.delete();
				}
				this.addActionError("删除成功！");
				return SUCCESS;
			}
		}
	}

	public String deleteAtt() throws Exception {
		AttachmentService attachmentservice = AttachmentFactory.getInstance()
				.creatAttachmentService();

		String title = attachmentservice.ExitAtt(attachment);
		if (title.equalsIgnoreCase("")) {
			this.addActionError("不存在该文件！");
			return INPUT;
		} else {
			if (!attachmentservice.DeleteAtt(attachment)) {
				this.addActionError("出错啦！");
				return INPUT;
			} else {
				String path = ServletActionContext.getServletContext()
						.getRealPath("images");
				setFileName(attachment.getTitle());
				File f = new File(path + "\\" + title);
				if (f.exists()) {
					f.delete();
				}
				this.addActionError("删除成功！");
				return SUCCESS;
			}
		}
	}

}
